package com.xebia.pomodoro.transformer
{
	import com.xebia.pomodoro.enums.P2PMessageType;
	import com.xebia.pomodoro.messages.ChatMessage;
	import com.xebia.pomodoro.messages.P2PMessage;
	import com.xebia.pomodoro.messages.PomodoroStartMessage;
	import com.xebia.pomodoro.messages.PomodoroStopMessage;
	import com.xebia.pomodoro.messages.PomodoroSyncMessage;
	import com.xebia.pomodoro.messages.PomodoroSyncRequestMessage;
	import com.xebia.pomodoro.messages.RegisterationMessage;
	import com.xebia.pomodoro.messages.WelcomeMessage;

	import flash.utils.Dictionary;

	import spark.components.Group;

	public class P2PMessageTransformer
	{
		private var messageTypeHandlerDict:Dictionary;

		public function P2PMessageTransformer()
		{
			messageTypeHandlerDict = new Dictionary();
			messageTypeHandlerDict[P2PMessageType.WELCOME] = handleWelcomeMessage;
			messageTypeHandlerDict[P2PMessageType.REGISTERATION] = handleRegisterationMessage;
			messageTypeHandlerDict[P2PMessageType.CHAT] = handleChatMessage;
			messageTypeHandlerDict[P2PMessageType.START_POMODORO] = handleStartPomodoroMessage;
			messageTypeHandlerDict[P2PMessageType.STOP_POMODORO] = handleStopPomodoroMessage;
			messageTypeHandlerDict[P2PMessageType.SYNC_POMODORO_REQUEST] = handleSyncPomodoroRequestMessage;
			messageTypeHandlerDict[P2PMessageType.SYNC_POMODORO] = handleSyncPomodoroMessage;

		}

		public function transformMessageObj(message:Object):P2PMessage
		{
			var messageType:String = message.type;
			var p2pMessage:P2PMessage = messageTypeHandlerDict[messageType].call(this, message);
			return p2pMessage;
		}

		private function handleWelcomeMessage(message:Object):P2PMessage
		{
			return new WelcomeMessage(message.username, message.groupName);
		}

		private function handleRegisterationMessage(message:Object):P2PMessage
		{
			return new RegisterationMessage(message.username, message.senderPeerId);
		}

		private function handleChatMessage(message:Object):P2PMessage
		{
			var chatMessage:ChatMessage = new ChatMessage(message.username);
			chatMessage.messageText = message.messageText;
			return chatMessage;
		}

		private function handleStartPomodoroMessage(message:Object):P2PMessage
		{
			return new PomodoroStartMessage(message.username, message.minutes);
		}

		private function handleStopPomodoroMessage(message:Object):P2PMessage
		{
			return new PomodoroStopMessage(message.username);
		}

		private function handleSyncPomodoroRequestMessage(message:Object):P2PMessage
		{
			return new PomodoroSyncRequestMessage(message.senderPeerId, message.username);
		}

		private function handleSyncPomodoroMessage(message:Object):P2PMessage
		{
			return new PomodoroSyncMessage(message.hasPomodoroStarted, message.minutes, message.username);
		}


	}
}
